<template>
  <view class="home">
    <text class="name">{{ title }}</text>
    <button @click="toUploadFile">上传图片</button>
  </view>
</template>

<script lang="ts">
  import { defineComponent, ref } from '@vue/composition-api';
  import { getExample, uploadImage } from '@/api';

  export default defineComponent({
    setup() {
      const title = ref('示例界面');
      (async function create() {
        const exampleData = await getExample();
        console.log(exampleData);
      })();

      function toUploadFile() {
        uni.chooseImage({
          count: 1, //上传图片的数量，默认是9
          sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
          sourceType: ['album'], //从相册选择
          success: function (res) {
            const tempFilePaths = res.tempFilePaths; //拿到选择的图片，是一个数组
            console.log(tempFilePaths[0]);
            uploadImage(tempFilePaths[0])
              .then((res) => {
                console.log(res);
              })
              .catch((err) => {
                console.log(err);
              });
          }
        });
      }
      return {
        title,
        toUploadFile
      };
    }
  });
</script>

<style lang="scss" scoped>
  .home {
    .name {
      font-size: 12rpx;
    }
  }
</style>
